// views/index/silkbag/index.js
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    _num: null,
    idea: '',
    showloading: true,
    file1: ''
  },
  onReady: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/ideas/index.html`,
      method: "POST",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        res.data.forEach(function (item, index) {
          item.info.size = Math.floor(item.info.size / 1024 / 1024 * 100) / 100
          item.info.txt = `${BASE_URL}/uploads/jinnang/` + item.info.txt
          item.list = []
          item.fen_img = `${BASE_URL}/uploads/jinnang/` + item.fen_img,
            item.author = item.author.split("|")
          item.author.forEach(function (itemm, index) {
            itemm = itemm.split(",")
            itemm[0] = `${BASE_URL}/uploads/jinnang/m_` + itemm[0]
            item.list.push(itemm)
          })
        })
        _this.setData({
          idea: res.data,
          _num: 0,
          showloading: false
        })
      }
    })
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
    console.log(this.data.value)
  },
  handleNav: function (e) {
    this.setData({
      _num: e.currentTarget.dataset.num
    })
  },
  downfile: function (e) {
    
    wx.showLoading({
      title: '下载中...'
    })
    wx.downloadFile({
      url: e.currentTarget.dataset.url,
      success: function (res) {
        wx.hideLoading()
        wx.showToast({
          title: '下载成功',
          icon: 'success',
          duration: 2000
        })
        const tempFilePath = res.tempFilePath;

        console.log(tempFilePath)
        // 保存文件
        wx.saveFile({
          tempFilePath,
          success: function (res) {
            console.log(res)
            const savedFilePath = res.savedFilePath;
            // 打开文件
            wx.openDocument({
              filePath: savedFilePath,
              showMenu: true,
              success: function (res) {
                console.log(res)
                console.log('打开文档成功')
              },
            });
          },
          fail: function (err) {
            console.log('保存失败：', err)
          }
        });
      },
      fail: function (err) {
        wx.showToast({
          title: '下载失败',
          icon: 'error',
          duration: 2000
        })
        console.log('下载失败：', err);
      },
    });
  }
})